#!/bin/bash
# the next line restart using tclsh \
exec tclsh "$0" "$@"

source "NirvanixAPI.tcl"

# SET PARAMS
set params { "appkey" "YOUR-APP-KEY"
 "username" "YOUR-ACCOUNT"
 "password" "YOUR-PASSWORD"
}

# GET RESULT
puts stdout "Starting Nirvanix Login..."

set results [nirvanix::executeMethod "Authentication" "Login" $params false]
puts stdout "Response Code: [nirvanix::getValue $results ResponseCode]"
set sessionToken [nirvanix::getValue $results SessionToken]

puts stdout "Session Token: $sessionToken"

set params { "sizeBytes" "1000"
	"destFolderPath" "/"
	"fileOverwrite" "true"
}
lappend params "sessionToken" $sessionToken
set results [nirvanix::executeMethod "IMFS" "GetStorageNodeExtended" $params false]

#puts stdout "Results: $results"
puts stdout "Response Code: [nirvanix::getValue $results ResponseCode]"
set UploadHost [nirvanix::getValue $results UploadHost]
puts stdout "Upload Host: $UploadHost"
set UploadToken [nirvanix::getValue $results UploadToken]
puts stdout "Upload Token: $UploadToken"

# UPLOAD FILE
#nirvanix::uploadFile "/alocalfile.txt" "/" "remotefilename.txt" $UploadHost $UploadToken ""

# DOWNLOAD FILE
#"/NirvanixAPI.tcl"
set params { "filePath" "/remotefilename.txt"
	"expiration" "10000"
}
lappend params "sessionToken" $sessionToken
set results [nirvanix::executeMethod "IMFS" "GetOptimalUrls" $params false]

nirvanix::downloadFile "testdownloadfile.txt" [nirvanix::getValue $results DownloadURL] ""




